<?php

include_once 'index.php';

//要基于id而不是limit去遍历新闻表 否则到后面会有大量冗余的遍历
$mysqli = DBMysqli::getInstance();
$res = $mysqli->query("select * from `news` limit 10");
$newsList = $res->fetch_all(MYSQLI_ASSOC);

$redis = CacheRedis::getInstance();

if(!empty($newsList))
{
    foreach($newsList as $news)
    {
        $nid = $news['id'];
        $res = $mysqli->query("select count(*) as `c` from `news_comment` where `nid`={$nid}");
        if(!$res)
        {
            $count = 0;
        }
        else
        {
            $arr = $res->fetch_assoc();
            $count = $arr['c'];
        }
        $key = Consts::KEYPREFIX_NID_LITE . $nid;
        $redis->hSet($key, 'commentcount', $count);
    }
}
